package
{
	import org.cove.ape.*;
	
	import flash.display.Stage;
	import flash.display.Sprite;
	
	//draw a little
    import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.Shape;


	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import flash.utils.*;  
	import flash.text.*;  
	
	import caurina.transitions.Tweener;

	
	[SWF(width="650", height="350", backgroundColor="#000000")] 
	public class SmallCars extends Sprite
	{
		//this will go away
        private var bgColor:uint      = 0xFFCC00;
        private var borderColor:uint  = 0x666666;
   
		//framerate text vars here
	    private var lastFrameTime:Number;
    	private var output:TextField;

		private function runApe(evt:Event):void
		{
				APEngine.step();
				APEngine.paint();	
		}
		
		public function SmallCars( )
		{
			
			stage.frameRate = 60;

			//create a container
            var _container:Sprite = new Sprite();
			addChild(_container);

			//draw a few guides                        
            var child2:Shape = new Shape();
            child2.graphics.beginFill(0xFFCCAA);
            child2.graphics.lineStyle(0, 0x666666);
            child2.graphics.drawRoundRect(0, 0, 5, 5, 8);
            child2.graphics.endFill();
            addChild(child2);//la "radacina"




			//showing framerate
			output = new TextField();
			output.autoSize = TextFieldAutoSize.LEFT;
			output.border     = false;
			output.background = false;
			output.textColor = 0xFFFFFF;
			output.selectable = false;
			addChild(output);

			addEventListener(Event.ENTER_FRAME, enterFrameListener);

			//Engine init phase
			//for now it's done global
			APEngine.init(1/4);
			APEngine.container = _container;
			APEngine.addMasslessForce(new Vector(0, 3));

			//this is where we set up MVC
			var model:IModel = new Model();
			var controller:Controller = new Controller(model);
			var view:View = new View(model, controller, this.stage,_container);
			//we need to pan the view 
			controller.addView(view);

			var miniView:MiniView = new MiniView(model, this.stage);

			addEventListener(Event.ENTER_FRAME, this.runApe );
			
		}
		public function enterFrameListener (e:Event):void 
		{
			var now:Number = getTimer();
			var elapsed:Number = now - lastFrameTime;
			var framesPerSecond:Number = Math.round(1000/elapsed);
			output.text = "actual frame rate: " + framesPerSecond
             + "\n[a], [d] panning, [<-] [->] Arrows move"
             + "\n [1-left] [2-neutral] [3-brakes] [4-neutral] [5-right]";
            lastFrameTime = now;
    	}
	}
}